package genetic.individual;

import genetic.individual.fitness.MarioIndividualFitnessPatterns;

public class MarioIndividual extends MarioIndividualAbstract<Double> {

	
	public MarioIndividual(int numberOfGenes, int supLimit, boolean randomStart){
		super(numberOfGenes, supLimit, randomStart);
		fitness = 0.0;
	}

	public MarioIndividual(MarioIndividual ind){
		super(ind);
	}

	public MarioIndividual copy(){
		return new MarioIndividual(this);
	}

	@Override
	protected Double copyFitness(Double fitness){
		return new Double(fitness);
	}

	@Override
	protected Double evalImplementation() {
		int[][] patterns = MarioIndividualFitnessPatterns.PATTERNS;
		int[]   w = MarioIndividualFitnessPatterns.WEIGHTS;

		//Procurar padrões
		double found = 0;
		Double npatterns = 0.0;
		for (int p = 0; p < patterns.length; p++) {
			int i = 0;
			for (int j = 0; j < genes.length; j++) {
				if (patterns[p][i] == genes[j]){
					i++;
				}else if(i != 0){
					found += (((double)i/patterns[p].length) > 0.5) ? ((double)i/patterns[p].length) : 0.0;
					i = 0;
					j--;
				}
				if(i == patterns[p].length){
					found++;
					i = 0;
				}
			}
			npatterns += found*w[p];
			found = 0;
		}
		
		return npatterns;
	}
	
}

